package manage

import (
	"context"
	"gozero-demo/internal/constants"
	"gozero-demo/internal/repo/mysql"

	"gozero-demo/internal/svc"
	"gozero-demo/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type OrdersInfoLogic struct {
	logx.Logger
	ctx        context.Context
	svcCtx     *svc.ServiceContext
	ordersRepo *mysql.OrdersRepo
}

// 订单详情
func NewOrdersInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *OrdersInfoLogic {
	return &OrdersInfoLogic{
		Logger:     logx.WithContext(ctx),
		ctx:        ctx,
		svcCtx:     svcCtx,
		ordersRepo: mysql.NewOrdersRepo(ctx, svcCtx),
	}
}

func (l *OrdersInfoLogic) OrdersInfo(req *types.OrdersListRequest) (resp *types.CommonResponse, err error) {
	listLogic := NewOrdersListLogic(l.ctx, l.svcCtx)
	_, listData, err := listLogic.OrdersListData(req)
	if err != nil {
		logx.WithContext(l.ctx).Errorf("failed to get order list data: %v", err)
		return types.ErrorResponse(constants.CodeServerError.Code, err.Error(), l.ctx), err
	}
	if len(listData) == 0 {
		return types.SuccessResponse(nil, l.ctx), nil
	}
	return types.SuccessResponse(listData[0], l.ctx), nil
}
